package com.google.code.timetrail.gui;

import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import com.google.code.timetrail.backend.Control;
import com.google.code.timetrail.backend.Doctor;
import com.google.code.timetrail.backend.Engineer;
import com.google.code.timetrail.backend.Entrepreneur;
import com.google.code.timetrail.backend.Inventory;
import com.google.code.timetrail.backend.Nutritionist;
import com.google.code.timetrail.backend.Person;

public class StartupFrameBackend {
    private Control gameControl;
    private ArrayList<String> additionalPlayers;
    private int addButtonCount;
    private String addedPlayer;
    private JFrame frame;
    private String selectedProfession;
    private String playerName;
    private Person player;
    private Inventory playerInv;
    private int selectedProfessionIndex;

    public StartupFrameBackend(Control gameControl){
        this.gameControl = gameControl;
        additionalPlayers = new ArrayList<String>();
        selectedProfessionIndex = -1;
    }

    public String getTitleLabel() {
        // TODO Auto-generated method stub
        return "Time Trail \n";
    }

    public String getEnterNameLabel() {
        // TODO Auto-generated method stub
        return "Enter Name:";
    }

    public String getDefaultPlayerName() {
        // TODO Auto-generated method stub
        return "Jake Gibel";
    }

    public String getAddMembersLabel() {
        // TODO Auto-generated method stub
        return "Enter Name of additional Party members:";
    }

    public String getAddButtonText() {
        // TODO Auto-generated method stub
        return "Add Player";
    }

    public void pressAddButton() {
        // TODO Auto-generated method stub
        if(addButtonCount<4){
            String s = JOptionPane.showInputDialog(
                                frame,
                                "Enter Name of additional Party member",
                                "Name"
                                );
            
            if ((s != null) && (s.length() > 0)) {
                addedPlayer = s;
                additionalPlayers.add(s);
                addButtonCount++;
                return;
            }
        }
        
    }

    public String getAddedPlayer() {
        // TODO Auto-generated method stub
        return addedPlayer;
    }
    public boolean canAddPlayer(){
        return addedPlayer != null || addedPlayer != "";
    }

    public void professionValueChanged(int selectedIndex) {
        this.selectedProfessionIndex = selectedIndex;
        // TODO Auto-generated method stub
        if(selectedIndex == 0){
            selectedProfession = "Engineer";
        } else if(selectedIndex == 1){
            selectedProfession = "Nutritionist";
        } else if(selectedIndex == 2){
            selectedProfession = "Doctor";
        } else if(selectedIndex == 3){
            selectedProfession = "Entrepreneur";
        }
        
    }

    public String[] getProfessionValues() {
        // TODO Auto-generated method stub
        return new String[] {"Engineer", "Nutritionist", "Doctor", "Entrepreneur"};
    }

    public String getProfessionLabelText() {
        // TODO Auto-generated method stub
        return "Choose Profession:";
    }

    public String getNextButtonText() {
        // TODO Auto-generated method stub
        return "Next >>";
    }

    public void setNameText(String text) {
        // TODO Auto-generated method stub
        playerName = text;
    }

    public boolean pressNextButton() {
        // TODO Auto-generated method stub
        if(playerName.equals("") || playerName == null || selectedProfessionIndex == -1){
            JOptionPane.showMessageDialog(frame,
                    "Please fill in all required information.",
                    "Some fields were left empty",
                    JOptionPane.PLAIN_MESSAGE);
            return false;
        } else {
            if(selectedProfession.equals("Engineer")){
                player = new Engineer();
            } else if(selectedProfession.equals("Nutritionist")){
                player = new Nutritionist();
            } else if(selectedProfession.equals("Doctor")){
                player = new Doctor();
            } else if(selectedProfession.equals("Entrepreneur")){
                player = new Entrepreneur();
            }
            ArrayList<Person> members = new ArrayList<Person>();
                for(int i = 0; i < additionalPlayers.size(); i++){
                    members.add(new Person(additionalPlayers.get(i)));
                }
            
            player.setName(playerName);
            playerInv = new Inventory();
            player.initialBonus(playerInv);
            gameControl.setInv(playerInv);
            gameControl.setPlayer(player);
            gameControl.setMembers(members);
            return true;
        }
    }
}
